//
//  WBVisitorView.swift
//  swiftweibo
//
//  Created by ffxk007 on 2017/6/17.
//  Copyright © 2017年 ffxk007. All rights reserved.
//

import UIKit
///访客视图
class WBVisitorView: UIView {
    
    
    ///注册按钮
     lazy var registerButton:UIButton = {
        let btn = UIButton()
        
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setTitle("注册", for: .normal)
        btn.setImage(UIImage(named:""), for: .normal)
        btn.sizeToFit()
        //        btn.backgroundImage(for: <#T##UIControlState#>)
        return btn
    }()
    ///登录按钮
     lazy var loginButton:UIButton = {
        let btn = UIButton()
        
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setTitle("登录", for: .normal)
        btn.setImage(UIImage(named:""), for: .normal)
        btn.sizeToFit()
        //        btn.backgroundImage(for: <#T##UIControlState#>)
        return btn
    }()

    
    var visitorInfo: [String:String]? {
        didSet{
            //去字典信息
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            //设置消息
            tipLabel.text = message
            tipLabel.textAlignment = .center
            //设置图像
            if imageName == "" {
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)
            
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    //MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 旋转图标动画
    fileprivate func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 5
        //动画完成不删除，如果iconview被删除，动画会一起销毁
        anim.isRemovedOnCompletion = false
        
        iconView.layer.add(anim, forKey: nil)
    }
    
//    //MARK: - 设置访客视图信息
//    func setupInfo(dic:[String:String]){
//    }
    
//    lazy var lablelll = { ()->UILabel in
//        let l = UILabel()
//        return l
//    }()
    //MARK: - 私有控件
    /// 图像视图
    fileprivate lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named:"tmng"))
    fileprivate lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "ting"))
    ///小房子
    fileprivate lazy var houseIconView:UIImageView = UIImageView(image: UIImage(named: ""))
    ///提示标签
    
    fileprivate lazy var tipLabel:UILabel = {
        
        let labell = UILabel()
        labell.text = "我们是你大爷，你信不信我们是你大爷，你信不信我们是你大爷，你信不信我们是你大爷，你信不信我们是你大爷，你信不信我们是你大爷，你信不信我们是你大爷，你信不信"
        labell.textColor = UIColor.darkGray
        labell.font = UIFont.systemFont(ofSize: 14)
        labell.sizeToFit()
        return labell
        
    }()
    
   
}

extension WBVisitorView{
    
    func setupUI(){
        backgroundColor = UIColor.red
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //q取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //自动布局
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier:1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier:1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        
//        let dic = ["2":1,"3":4]
        let metrics = ["spaceing":-35]
        let viewDict = ["maskIconView":maskIconView,"registerButton":registerButton] as [String : Any]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"H:|-0-[maskIconView]-0-|",
                                                      options: [],
                                                      metrics: nil,
                                                      views:viewDict))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"V:|-0-[maskIconView]-(spaceing)-[registerButton]",
                                                      options: [],
                                                      metrics: metrics,
                                                      views: viewDict))
        
      
    }
}
